package com.situ.vivamarket.intcp;


import com.situ.vivamarket.model.Member;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
//用户登录拦截器
public class MemberLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        HttpSession session = req.getSession();
        Member member = (Member) session.getAttribute("current_member_login");
        if (member == null) {
//            判断是否为ajax请求,因为ajax没办法实现重定向，参数是请求头的属性
            String header=req.getHeader("x-requested-with");
            if (header!=null&&header.equalsIgnoreCase("XMLHttpRequest")) {//是ajax
                resp.sendError(401);//401：权限未认证通过
                return false;
            }else{
                resp.sendRedirect(req.getContextPath()+"/vm/front/login");//跳到前端登录
                return false;
            }

        }else {
            return true;
        }
    }
}
